
use std::collections::HashMap;
fn main() {
    //创建HashMap
    let mut scores:HashMap<String,i32> = HashMap::new();
    scores.insert(String::from("xiaoming"),100);
    scores.insert(String::from("xiaogao"),150);

    let keys = vec![1,2,3];
    let values = vec![String::from("xiaoming"),String::from("xiaogao"),String::from("xiaotan")];
    //_占位符，iter()迭代器
    let scores:HashMap<_,_> = keys.iter().zip(values.iter()).collect();

    //读取,get返回的是一个Option类型，需要用if或match读取。
    let k = 1;
    match scores.get(&k) {
        Some(i) => println!("{}",i),
        None => println!("nothing")
    }

    if let Some(v) = scores.get(&k){
        println!("v = {}",v);
    }

    //遍历:会以任意的顺序遍历出来
    for (key,value)in &scores {
        println!("{}:{}",key,value);
    }

    //直接插入
    let mut ss = HashMap::new();
    ss.insert(1,String::from("one"));
    ss.insert(2,String::from("two"));
    ss.insert(3,String::from("three"));
    println!("{:?}",ss);

    //键不存在的时候插入
    ss.entry(4).or_insert(String::from("four"));
    ss.entry(3).or_insert(String::from("test"));
    println!("{:?}",ss);

    //根据旧值来更新一个值
    let text = "hello world wonderful world";
    let mut map = HashMap::new();
    for word in text.split_whitespace(){
        let count = map.entry(word).or_insert(0);
        *count += 1;
    }

    println!("map = {:?}",map);



    println!("Hello, world!");
}
